<%-- Copyright (c) 2005 BroadVision, Inc. All rights reserved              --%>
<%-- The product described in this manual may be protected by one or more  --%>
<%-- U.S. and International patents. The BroadVision One-To-One software   --%>
<%-- is covered by U.S. patent 5,710,887.                                  --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and the BroadVision logo are registered       --%>
<%-- trademarks, and BroadVision One-To-One is a trademark of BroadVision, --%>
<%-- Inc. IONA and Orbix are trademarks of IONA Technologies, Ltd. RSA,    --%>
<%-- MD5, and RC2 are trademarks of RSA Data Security, Inc. All other      --%>
<%-- trademarks, service marks, and trade names belong to their respective --%>
<%-- owners. BroadVision, Inc. disclaims any proprietary interest in the   --%>
<%-- marks and names of others.                                            --%>
<%--                                                                       --%>

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/bv-html.tld" prefix="bvhtml" %> 

<%@include file="/mt/common/pickerPopupHelper.jsp" %>

<%-- This holds the current notification type. "email" or "alert". --%>
<c:set var="notifType" value="${notifType}" />

<c:if test='${notifType == "email"}'>
  <c:set var="viewBean" value="${emEmailMessageEditView}" />
  <c:set var="formBean" value="${emEmailMessageEditForm}" />
  <c:set var="formBeanName" value="emEmailMessageEditForm" />
  <c:set var="actionPath" value="/em/notif/emailMessageSave" />
</c:if>
<c:if test='${notifType == "alert"}'>
  <c:set var="viewBean" value="${emAlertMessageEditView}" />
  <c:set var="formBean" value="${emAlertMessageEditForm}" />
  <c:set var="formBeanName" value="emAlertMessageEditForm" />
  <c:set var="actionPath" value="/em/notif/alertMessageSave" />
</c:if>

<c:set var="formBeanMap" value="${formBean.map}" />
<c:set var="viewBeanMap" value="${viewBean.map}" />

<% String ctxPath = request.getContextPath(); %>
<script language="javascript" src="<%= ctxPath %>/mc/js/date.js"></script>

<SCRIPT language="javascript1.2">
	function checkMaxLength(taText,len) {
	    if (taText.value.length > len)
	        taText.blur();
	}

	// function to POST form parameters to a ".do"
	function doAction(action, myform) {
	    myform.action='<html:rewrite page="'+action+'" />';
	    myform.submit();
	    return true;
	}

    // When clicking cancel button, it will forward to the view message page of the current email,
    // or the current alert.
	function doCancel(myform) {
	  <c:choose>
        <c:when test='${notifType == "email"}'>
	      myform.action='<html:rewrite page="/em/notif/emailMessageView.do" />';
	    </c:when>
        <c:when test='${notifType == "alert"}'>
	      myform.action='<html:rewrite page="/em/notif/alertMessageView.do" />';
	    </c:when>
	  </c:choose>
 	  myform.submit();
	  return true;
	}      
	
    // When clicking reset button, it will forward to the edit message page of the current email,
    // or the current alert.
	function doReset(myform) {
	  <c:choose>
        <c:when test='${notifType == "email"}'>
	      myform.action='<html:rewrite page="/em/notif/emailMessageEdit.do" />';
	    </c:when>
        <c:when test='${notifType == "alert"}'>
	      myform.action='<html:rewrite page="/em/notif/alertMessageEdit.do" />';
	    </c:when>
	  </c:choose>
	  myform.submit();
	  return true;  	  
	}

    /**
     * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
     * http://javascript.internet.com
     */
    function checkEmail(emailStr) {
       if (emailStr.length == 0) {
           return true;
       }
       var emailPat=/^(.+)@(.+)$/;
       var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
       var validChars="\[^\\s" + specialChars + "\]";
       var quotedUser="(\"[^\"]*\")";
       var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
       var atom=validChars + '+';
       var word="(" + atom + "|" + quotedUser + ")";
       var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
       var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
       var matchArray=emailStr.match(emailPat);
       if (matchArray == null) {
           return false;
       }
       var user=matchArray[1];
       var domain=matchArray[2];
       if (user.match(userPat) == null) {
           return false;
       }
       var IPArray = domain.match(ipDomainPat);
       if (IPArray != null) {
           for (var i = 1; i <= 4; i++) {
              if (IPArray[i] > 255) {
                 return false;
              }
           }
           return true;
       }
       var domainArray=domain.match(domainPat);
       if (domainArray == null) {
           return false;
       }
       var atomPat=new RegExp(atom,"g");
       var domArr=domain.match(atomPat);
       var len=domArr.length;
       if ((domArr[domArr.length-1].length < 2) ||
           (domArr[domArr.length-1].length > 3)) {
           return false;
       }
       if (len < 2) {
           return false;
       }
       return true;
    }
	
	// When clicking save button, it will forward to the action to save the current email or alert.
	function doSave (myform) {
      if (validateForm(myform)) {
        if ( ! checkEmail(myform.from.value)) {
          <c:set var="fromEmail">
            <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.label.from" />
          </c:set>
          alert('<bean-el:message bundle="em" key="errors.email" arg0="${fromEmail}"/>');
          return false;
        } 
        if ( ! checkEmail(myform.replyTo.value)) {
          <c:set var="replyToEmail">
            <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.label.replyTo" />
          </c:set>
          alert('<bean-el:message bundle="em" key="errors.email" arg0="${replyToEmail}"/>');
          return false;
        }
      
        checkIfSettingToUnscheduled (myform);
        
	    <c:choose>
          <c:when test='${notifType == "email"}'>
	      myform.action='<html:rewrite page="/em/notif/emailMessageSave.do" />';
	      </c:when>
          <c:when test='${notifType == "alert"}'>
	      myform.action='<html:rewrite page="/em/notif/alertMessageSave.do" />';
	      </c:when>
	    </c:choose>
        myform.submit();
	    return true;	    
      }
      else {
        return false;
      }
	}	
	
	// When clicking the personalize text button, it will show a pop-up window, that contains
	// the visitor's attributes for copy and paste.
	function showAttributes() {
	   var url = '<html:rewrite page="/em/notif/showAttributesPopup.do"/>';
       var win = window.open(url,"Email",
 			"directories=no,height=300,width=450,left=50,top=50,resizable=yes,scrollbars=yes");

       if (win != null){
           if (win.opener == null)        
               win.opener = self;
        }
       win.focus();
	}

    // call back function for pickerPopupUncategorized.do.
	function selectSegment(selectedSegmentOids, selectedSegmentNames) {
      if ((selectedSegmentOids.length > 0) && 
          (selectedSegmentNames.length > 0)) {
        <c:choose>
        <c:when test='${notifType == "email"}'>
          theForm = document.emEmailMessageEditForm;
        </c:when>
        <c:when test='${notifType == "alert"}'>
          theForm = document.emAlertMessageEditForm;
        </c:when>
        </c:choose>
	    theForm.segmentName.value = selectedSegmentNames[0];
	    theForm.segmentId.value = selectedSegmentOids[0];
	  }
	}

    function onSelectJspFile (myform) {
      myform.scriptBody.disabled=true;
      myform.jspFileName.disabled=false;
      myform.personalizeTextButton.disabled = true;
    }

    function onSelectTextInput (myform) {
      myform.jspFileName.disabled=true;
      myform.scriptBody.disabled=false;
      myform.personalizeTextButton.disabled = false;
    }
    
    function checkIfSettingToUnscheduled (myform) {
      // look up the scheduledAndPassed value in view bean
      // if true, prompt user if they want to change to Unscheduled status
      // ok : set the hidden field of changeToUnscheduled to true
      // cancel: set the hidden field of changeToUnscheduled to false
      // return
      <c:choose>
        <c:when test="${viewBeanMap.scheduledAndPassed}">
          var turnOffline = false;
          turnOffline = confirm('<bean-el:message key="em.notif.messageEdit.confirm.changeToUnscheduled" bundle="em"/>');
          myform.changeToUnscheduled.value = turnOffline;
        </c:when>
      </c:choose>
    }
       
</SCRIPT>

<html-el:javascript formName="${formBeanName}" method="validateForm" />

<!--start zone 5 content area-->
<table width="100%" border="0" cellspacing="0" cellpadding="0"
       class="zoneFive">
  <tr> 
    <td valign="top" width="100%">
      <div class="pageTitle">
        <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.pageTitle"/> :
           <c:out value="${viewBeanMap.name}" />
      </div>
      <div class="errorMessage"><html:errors bundle="em"/></div>
      
      <div><jsp:include page="/mc/common/pagetab.jsp" flush="true" /></div>
            
      <div class="pageInstructions">
        <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.pageInstruction"/>
      </div>
              
      <div class="pageSubtitle">
        <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.pageSubtitle.messageHeader"/>
      </div>
      
      <!--start main content here-->
      <html-el:form action="${actionPath}">
        <html:hidden property="oid"/>
        <html:hidden property="changeToUnscheduled"/>      
      <table width="100%" border="0" cellspacing="0" cellpadding="0" class="labelForm">

        <!-- edit message header starts here -->
        <tr> 
          <td class="label" width="20%">
            <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.label.from"/>*: 
          </td>
          <td class="labelField">
            <html:text property="from" size="30"/>
          </td>
        </tr>
          
        <tr>        
          <td class="label" width="20%"> 
            <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.label.replyTo"/>: 
          </td>
          <td class="labelField">
            <html:text property="replyTo" size="30"/>
          </td>     
        </tr>
        
        <%-- Only show segment selection if this is an email --%>
        <c:if test='${notifType == "email"}'>
        <tr>
          <td class="label" width="20%"> 
            <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.label.toSegment"/>*: 
          </td>
          <td class="labelField">
            <html:text property="segmentName" size="30" readonly="true"/>
            <html:hidden property="segmentId"/>
            
			<!--Start segment browse button -->
			<%-- When clicks the browse segment button, it will show the list 
			     of uncategorized segments in the pop-up window. --%>
			<input type="button" class="inputButton" 
			  value="<bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.button.browseSegments"/>"
              onclick="doPickerPopupAction('<html:rewrite page="/mt/pickerPopupUncategorized.do"/>', 
                                           'selectSegment', 
                                           <c:out value="${viewBeanMap.segmentContentTypeId}" />, 
                                           null, 
                                           null,
                                           'SEGMENT_NAME',
                                           true,
                                           false,
                                           true,
                                           null,
                                           null,
                                           1);"/>
            <!-- End segment browse button -->            
          </td>
        </tr>
        </c:if>
        
        <tr>        
          <td class="label" width="20%"> 
            <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.label.subject"/>*: 
          </td>
          <td class="labelField">
            <html:text property="subject" size="30"/>
          </td>     
        </tr>
        <!-- edit message header ends here -->
      </table>

      <div class="pageSubtitle">
        <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.pageSubtitle.messageBody"/>
      </div>
      
      <div class="pageInstructions">
        <bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.bodyInstruction"/>
      </div>      

      <table width="100%" border="0" cellspacing="0" cellpadding="0" class="labelForm" summary="Message body">
		<tr>
		  <td class="label" width="20%" nowrap>
            <bean-el:message bundle="em" key="em.notif.messageEdit.label.selectMessageSource"/>:		  
		  </td>
          <td class="labelField" width="80%">
            <%-- If the choice is text input for script body (0), disable the jsp file dropdown,
                 if the choice is jsp file (2), disable the script text area input. --%>
            <c:set var="disableJspFileDropDown" value="${formBeanMap.scriptForm == 0}"/>
            <c:set var="disableScriptBodyTextArea" value="${formBeanMap.scriptForm == 2}"/>           
            <div class="labelField" width="80%">
              <%-- Choice of jsp file --%>     
              <html:radio property="scriptForm" value="2" onclick="onSelectJspFile(this.form)"/>
			  <bean-el:message bundle="em" key="em.notif.messageEdit.label.fromJspFile"/>:
			  <html-el:select 
                 disabled="${disableJspFileDropDown}"		  
                 property="jspFileName">
                 <html:option value=""><bean-el:message bundle="em" key="em.option.select"/></html:option>
			    <%-- the labels and values of the jspFileNames drop down are the same. --%>
				<html:options name="viewBean" property="jspFileNames"/>
			  </html-el:select>		
			</div>
			
			<div class="labelField" width="80%">
			  <%-- Choice of plain text input --%>
			  <html:radio property="scriptForm" value="0" onclick="onSelectTextInput(this.form)"/>
			  <bean-el:message bundle="em" key="em.notif.messageEdit.label.fromPlainTextInput"/>:
                <br><br>
			    <html-el:textarea
                   disabled="${disableScriptBodyTextArea}"		    
	               property="scriptBody" rows="6" cols="80" />
                <br>
                <input type="button" class="inputButton" name ="personalizeTextButton"
			         value="<bean-el:message bundle="em" key="emarketing.notif.emailMessageEdit.button.personalizeText"/>" 
			         onclick="showAttributes()"/>               
			    <br>                   
			</div>
          </td>
        </tr>
      </table>

      <div class="pageButtonWrap" align="right">          
        <input type="button"    name="resetButton"
                      class="inputButton"
                      value="<bean-el:message bundle="em" key="button.reset"/>"
                      onClick="doReset(this.form);">&nbsp; &nbsp;
	    <input type="button" name="cancelButton"
                      class="inputButton" 
                      value="<bean-el:message bundle="em" key="button.cancel"/>"
                      onClick="doCancel(this.form);"/>
        <input type="button" name="saveButton"
                      class="inputButton"
                      value="<bean-el:message bundle="em" key="button.save"/>"
                      onClick="doSave(this.form);"/>
      </div>
  	  </html-el:form>
  	
    </td>
  </tr>

<!--end zone 5 content area-->
</table>

